<?php
namespace App\Controllers\Admin;

use App\Controllers\Controller;
use App\Logics\MenuLogic;
use Core\Request;
use Core\WebSession;

class MenuController extends Controller {

    private $logic;

    public function getJson(){
        $list = $this->logic->getTreeList();
        $this->success($list, 'success');
    }

    public function __construct(){
        $this->logic = new MenuLogic();
    }

    public function index(){
        $list = $this->logic->getAll();
        $data = [
            'menu_json' => json_encode($list)
        ];
        $this->view(__FUNCTION__, $data);
    }


    private function getInfoParentId(){
        $key = 'menu_parent_id';
        $classId = WebSession::getInstance()->get($key);
        if(empty($classId)){
            return 0;
        }
        return $classId;
    }

    private function setInfoParentId($value){
        $key = 'menu_parent_id';
        return WebSession::getInstance()->set($key, $value);
    }

    public function add(){
        $menus = $this->logic->getTreeList();
        $data = ['id' => 0, 'menus' => $menus];
        $parent_id = input('parent_id',  $this->getInfoParentId());
        $parentData = [
            'title' => '顶级菜单',
            'id' => 0
        ];
        if($parent_id > 0){
            $parentData = $this->logic->getDetail($parent_id);
        }
        $data['menu_parent'] = $parentData;
        $data['menu_json'] = json_encode($menus);
        $this->view('add', $data);
    }

    public function edit(){
        $id = request()->getInt('id');
        $data = $this->logic->getDetail($id);
        $parentData = [
            'title' => '顶级菜单',
            'id' => 0
        ];
        if($data['parent_id'] > 0){
            $parentData = $this->logic->getDetail($data['parent_id']);
        }
        $menus = $this->logic->getTreeList();
        $menuJson = json_encode($menus);
        $this->view('edit', ['menu' => $data,'menu_json' => $menuJson, 'parent_menu' => $parentData, 'id' => $id]);
    }

    public function save(){
        $data = \request()->all();
        //log_error('article', $data);
        $id = $data['id'];
        $this->logic->save($id, $data);
        //只有添加的时候，才记录上次添加ID
        if($id == 0){
            $this->setInfoParentId($data['parent_id']);
        }
        $message = $id == 0 ? '添加成功' : '修改成功';
        $this->success([], $message);
    }

    public function delete(){
        $id = I('get.id', 0);
        $this->logic->delete($id);
        $this->success();
    }

    /**
     * 设置上下架状态
     *
     * @author lichunguang 153102250@qq.com
     * @since 2022/4/27 下午2:25
     */
    public function setState(){
        $id = input('id', 0, 'intval');
        $state = input('state', 0, 'intval');
        try {
            $this->logic->setState($id, $state);
            $this->success();
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 设置排序
     *
     * @author lichunguang 153102250@qq.com
     * @since 2022/4/27 下午2:25
     */
    public function sort(){
        $id = input('id', 0, 'intval');
        $sort = input('sort', 0, 'intval');
        $this->logic->setSort($id, $sort);
        $this->success();
    }

}